package com.le.tester.book.concurrentchallenge.threadSafedelegation;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * createTime：2025-03-18 9:41
 * description：线程安全
 */
public class RightListHelper<E> {

    public List<E> list = Collections.synchronizedList(new ArrayList<>());

    public boolean putIfAbsent(E x) {
        //锁住线程可能读取到不正确的状态值
        synchronized (list) {
            boolean absent = !list.contains(x);
            if (absent) {
                list.add(x);
            }
            return absent;
        }
    }
}

